#! /usr/bin/env python
#coding=utf-8

import re
import datetime

def getLineList(file):
    f = open(file)
    lineList = f.readlines()
    f.close()
    
    return lineList

def match(lineList,srcFormate,destFormate):
    
    newLineList=[]
    for line in lineList:
        
        result = re.sub(srcFormate,destFormate,line)
        if result!=line:
            newLineList.append(result)
        else:
            newLineList.append('\n')
    return newLineList
    
    
def outputFile(filePath,newLineList):
    f = open(filePath,'w')
    [f.write(line) for line in newLineList]
    f.close()
    
    
def main(srcFile,\
        destFile=str(ur'E:\tmp\ab\tmp\%s.txt' % datetime.datetime.today().strftime('%Y%m%d%H%M%S%f')),\
        srcFormate=ur'^(.*)(http\:\/\/mp3\.180mp3\.com.*\.mp3)(.*)$',\
        destFormate=ur'\2'):
    
    list = getLineList(srcFile)
    matchList = match(list,srcFormate,destFormate)
    outputFile(destFile,matchList)
    
    

if __name__=='__main__':
    file = raw_input(ur'文件:')
    matchModel = raw_input(ur'匹配模式:')
    destModel = raw_input(ur'目标模式:')
    if len(matchModel)<1 or len(destModel)<1:
        main(file) 
    else:
        main(file,matchModel,destModel)
    #main(ur'E:\tmp\ab\tmp\dt.js',ur'E:\tmp\ab\tmp\newdt.js',ur'^(.*)(http\:\/\/mp3\.180mp3\.com.*\.mp3)(.*)$',ur'\2')
